<?php

namespace app\commi\controller;

use app\admin\model\UserType;
use think\Controller;
use think\Db;
use org\Verify;
use com\Geetestlib;

class Login extends Controller
{
    //登录页面
    public function index()
    {
        return $this->fetch('/login');
    }

    //登录操作 1：数字验证码  2：极验验证
    public function doLogin()
    {
        $cm_login_ID = input("cm_login_ID");
        $cm_login_pwd = input("cm_login_pwd");
        if (config('verify_type') == 1) {
            $code = input("code");
        }
        $verify = new Verify();
        if (config('verify_type') == 1) {
            if (!$code) {
                return json(['code' => -4, 'url' => '', 'msg' => '请输入验证码']);
            }
            
            if (!$verify->check($code)) {
                return json(['code' => -4, 'url' => '', 'msg' => '验证码错误','status'=>100]);
            }
            
           return  $this->checkAdmin($cm_login_ID,$cm_login_pwd);
        }elseif (config('verify_type') == 2) {

            $GtSdk = new Geetestlib(config('gee.gee_id'), config('gee.gee_key'));
            $user_id = session('user_id');
            if (session('gtserver') == 1) {
                $result = $GtSdk->success_validate(input('param.geetest_challenge'), input('param.geetest_validate'), input('param.geetest_seccode'), $user_id);
                //极验服务器状态正常的二次验证接口
                if (!$result) {
                    $this->error('请先拖动验证码到相应位置');
                }
            }else{
                if (!$GtSdk->fail_validate(input('param.geetest_challenge'), input('param.geetest_validate'), input('param.geetest_seccode'))) {
                    //极验服务器状态宕机的二次验证接口
                    $this->error('请先拖动验证码到相应位置');
                }
            }
            return  $this->checkAdmin($cm_login_ID,$cm_login_pwd);
        }else{
            return  $this->checkAdmin($cm_login_ID,$cm_login_pwd);
        }
    }

    //验证密码和账号
    public function checkAdmin($username,$password){

        $hasUser = Db::table('up_committee_member')
            ->where('cm_login_ID', $username)
            ->where('cm_login_pwd', md5($password))
            ->field('cm_id, cm_committee_id, cm_admin_name, cm_admin_phone, cm_login_pwd, 
            cm_member_type, cm_member_role')
            ->find();
        
        if(empty($hasUser)){
            return json(['code' => -1, 'url' => '', 'msg' => '该管理员不存在或账号密码错误', "status" => 100]);
        }
    
//        if(md5($password) != $hasUser['cm_login_pwd']){
//            writelog($hasUser['cm_id'], $username, '管理员【'.$username.'】登录失败：密码错误', 2);
//            return json(['code' => -2, 'url' => '', 'msg' => '密码错误', "status" => 100]);
//        }
        
        $committee = Db::table('up_committee')->where('com_id', $hasUser['cm_committee_id'])->find();
        
        if(!$committee){
            return json(['code' => -2, 'url' => '', 'msg' => '所属组委会不存在', "status" => 100]);
        }
        
        if($committee['com_status'] == 2){
            return json(['code' => -2, 'url' => '', 'msg' => '所属组委会已被禁用', "status" => 100]);
        }

        $config = api('Config/lists'); //读取站点配置
        if($config['web_site_close'] == 0){
            $this->error('站点已经关闭，请稍后访问');
            die;
        }

        session('cm_id', $hasUser['cm_id']);         //组委会管理员ID
        session('cm_admin_name', $hasUser['cm_admin_name']);  //组委会管理员名称
        session('cm_member_type', $hasUser['cm_member_type']);  //角色
        session('cm_member_role', $hasUser['cm_member_role']);  //分类  1 帆船   2 赛艇
        session('com_id',$committee['com_id']);     //组委会ID
        session('com_name',$committee['com_name']);     //组委会名称
//        writelog($hasUser['id'],session('username'),'管理员【'.session('username').'】登录成功',1);
        return json(['code' => 1, 'url' => url('commi/index/index'), 'msg' => '登录成功！']);
    }

    //验证码
    public function checkVerify()
    {
        $verify = new Verify();
        $verify->imageH = 36;
        $verify->imageW = 110;
		$verify->codeSet = '023456789';
		$verify->useZh = false;//使用中文验证码
        $verify->length = 4;
        $verify->useNoise = true;//是否添加杂点
        $verify->useCurve = false;//是否画混淆曲线
        $verify->useImgBg = false;//使用背景图片
        $verify->fontSize = 16;
        return $verify->entry();
    }


    //极验验证
    public function getVerify(){
        $GtSdk = new Geetestlib(config('gee.gee_id'), config('gee.gee_key'));
        $user_id = "web";
        $status = $GtSdk->pre_process($user_id);
        session('gtserver',$status);
        session('user_id',$user_id);
        echo $GtSdk->get_response_str();
    }


    //退出操作
    public function loginOut()
    {
//        writelog(session('cm_id'), session('cm_admin_name'), '退出登录',1);
        session('cm_id', null);
        cache('db_config_data',null);
        $this->redirect(url('commi/Index/index'));
    }
}